package com.aube.rvi;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.aube.rvi.net.ImageLoader;
import com.aube.rvi.net.ImageRequest;
import com.aube.rvi.net.Request;
import com.aube.rvi.net.RequestQueue;
import com.aube.rvi.net.ResponseListener;
import com.aube.rvi.net.Volley;
import com.aube.rvi.util.StringUtils;

/**
 * Created by huyaonan on 16/10/25.
 */
public class CommonDataLoader {

    private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.RGB_565;
    private static CommonDataLoader mInstance = null;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private ImageLoader.ImageCache mBitmapCache;

    private CommonDataLoader(Context context) {
        mRequestQueue = Volley.newRequestQueue(context, 5);

        mImageLoader = new ImageLoader(mRequestQueue, mBitmapCache = new BitmapCache());
    }

    public static CommonDataLoader getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new CommonDataLoader(context.getApplicationContext());
        }
        return mInstance;
    }

    public ImageLoader getImageLoader() {
        return mImageLoader;
    }

    public ImageLoader.ImageCache getImageCache() {
        return mBitmapCache;
    }

    public void stop() {
        try {
            if (mRequestQueue != null)
                mRequestQueue.stop();
            if (mImageLoader != null)
                mImageLoader.quit();
        } catch (Exception e) {

        }
    }

    public void start() {
        try {
            if (mRequestQueue != null)
                mRequestQueue.start();
        } catch (Exception e) {

        }
    }

    public void clearImgCache() {
        if (mImageLoader != null)
            mImageLoader.clear();
    }

    public void cancelRequest(Object tag) {
        if (mRequestQueue != null)
            mRequestQueue.cancelAll(tag);
    }

    public void cancelAllRequest() {
        if (mRequestQueue != null)
            mRequestQueue.cancelAll(new RequestQueue.RequestFilter() {

                @Override
                public boolean apply(Request<?> request) {
                    return true;
                }
            });
    }

    public void cancelAllHttpRequest() {
        if (mRequestQueue != null)
            mRequestQueue.cancelAll(new RequestQueue.RequestFilter() {

                @Override
                public boolean apply(Request<?> request) {
                    return true;
                }
            });
    }

    public ImageLoader getmImageLoader() {
        return mImageLoader;
    }

    /**
     * 全参数模式
     *
     * @param image
     * @param url
     * @param defaultRes
     * @param width
     * @param height
     */
    public void startImageLoader(ImageView image, String url, int defaultRes, int width, int height) {
        if (StringUtils.isBlank(url))
            return;
        if (width != 0 && height != 0)
            url = ImageParamUtils.getImgUrl(url, width, height);
        image.setTag(url);//(R.id.img_url, url);
        mImageLoader.get(url, image, defaultRes, 0, 0, false);
    }

    public void startImageLoader(ImageView image, String url, int width, int height) {
        startImageLoader(image, url, 0, width, height);
    }

    /**
     * ImageRequest模式，可以获取bitmap
     *
     * @param url
     * @param width
     * @param height
     * @param listener
     */
    public void startImageRequest(String url, int width, int height, final ResponseListener<Bitmap> listener) {
        if (StringUtils.isBlank(url))
            return;
        url = ImageParamUtils.getImgUrl(url, width, height);
        startImageRequest(new ImageRequest(url, listener, 0, 0, BITMAP_CONFIG));
    }

    public void startImageRequest(String url, int width, int height, final ResponseListener<Bitmap> listener, int timeOut) {
        if (StringUtils.isBlank(url))
            return;
        ImageRequest imgRequest = new ImageRequest(ImageParamUtils.getImgUrl(url, width, height), listener, 0, 0, BITMAP_CONFIG);
        imgRequest.setTimeOut(timeOut);
        startImageRequest(imgRequest);
    }

    public void startImageRequest(String url, String tag, int width, int height, final ResponseListener<Bitmap> listener) {
        if (StringUtils.isBlank(url))
            return;
        startImageRequest(new ImageRequest(ImageParamUtils.getImgUrl(url, width, height), tag, listener, 0, 0, BITMAP_CONFIG));
    }

    public void startImageRequest(String url, String tag, int width, int height, final ResponseListener<Bitmap> listener, boolean cacheOnly) {
        if (StringUtils.isBlank(url))
            return;
        ImageRequest request = new ImageRequest(ImageParamUtils.getImgUrl(url, width, height), listener, 0, 0, BITMAP_CONFIG);
        request.setOnlyCache(cacheOnly);
        request.setTag(tag);
        startImageRequest(request);
    }

    private void startImageRequest(ImageRequest request) {
        mImageLoader.get(request);
    }

    public void startHttpLoader(Request<?> request) {
        mRequestQueue.add(request);
    }

    public Bitmap getCachedBitmapLoader(String url) {
        if (StringUtils.isBlank(url)) {
            return null;
        }

        return mImageLoader.getCachedPic(url, 0, 0);
    }

    public void release() {
        this.mImageLoader = null;
        this.mRequestQueue = null;
        mInstance = null;
    }

}
